
/*
 * Copyright 2002-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 *版权所有2002-2018原作者。
 *
 *根据Apache许可证2.0版（“许可证”）获得许可；
 *除非符合许可证，否则不得使用此文件。
 *您可以在以下地址获取许可证副本：
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 *除非适用法律要求或书面同意，软件
 *根据许可证分发的内容是按“原样”分发的，
 *无任何明示或暗示的保证或条件。
 *有关管理权限和
 *许可证下的限制。
 */

package org.springframework.beans.factory;

import java.util.Iterator;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Stream;

import org.springframework.beans.BeansException;
import org.springframework.lang.Nullable;

/**
 * A variant of {@link ObjectFactory} designed specifically for injection points,
 * allowing for programmatic optionality and lenient not-unique handling.
 *
 * <p>As of 5.1, this interface extends {@link Iterable} and provides {@link Stream}
 * support. It can be therefore be used in {@code for} loops, provides {@link #forEach}
 * iteration and allows for collection-style {@link #stream} access.
 *
 * @author Juergen Hoeller
 * @since 4.3
 * @param <T> the object type
 * @see BeanFactory#getBeanProvider
 * @see org.springframework.beans.factory.annotation.Autowired
 */
/**
 *专门为注入点设计的{@link ObjectFactory}的变体，
 *允许编程可选性和宽松而非独特的处理。
 *
 *＜p＞从5.1开始，此接口扩展了｛@link Iterable｝并提供｛@linkStream｝
 *支持。因此，它可以在｛@code for｝循环中使用，提供｛@link#forEach｝
 *迭代，并允许集合样式｛@link#stream｝访问。
 *
 *@作者Juergen Hoeller
 *@自4.3
 *@param＜T＞对象类型
 *@see BeanFactory#getBeanProvider
 *@参见org.springframework.beans.factory.annotation.Autowired
 */
public interface ObjectProvider<T> extends ObjectFactory<T>, Iterable<T> {

	/**
	 * Return an instance (possibly shared or independent) of the object
	 * managed by this factory.
	 * <p>Allows for specifying explicit construction arguments, along the
	 * lines of {@link BeanFactory#getBean(String, Object...)}.
	 * @param args arguments to use when creating a corresponding instance
	 * @return an instance of the bean
	 * @throws BeansException in case of creation errors
	 * @see #getObject()
	 */
	/**
	 *返回对象的实例（可能是共享的或独立的）
	 *由这家工厂管理。
	 *＜p＞允许指定显式构造参数
	 *｛@link BeanFactory#getBean（String，Object…）｝的行。
	 *@param args创建相应实例时要使用的参数
	 *@return bean的实例
	 *创建错误时@throws BeansException
	 *@参见#getObject（）
	 */
	T getObject(Object... args) throws BeansException;

	/**
	 * Return an instance (possibly shared or independent) of the object
	 * managed by this factory.
	 * @return an instance of the bean, or {@code null} if not available
	 * @throws BeansException in case of creation errors
	 * @see #getObject()
	 */
	/**
	 *返回对象的实例（可能是共享的或独立的）
	 *由这家工厂管理。
	 *@返回bean的实例，如果不可用，则返回｛@code null｝
	 *创建错误时@throws BeansException
	 *@参见#getObject（）
	 */
	@Nullable
	T getIfAvailable() throws BeansException;

	/**
	 * Return an instance (possibly shared or independent) of the object
	 * managed by this factory.
	 * @param defaultSupplier a callback for supplying a default object
	 * if none is present in the factory
	 * @return an instance of the bean, or the supplied default object
	 * if no such bean is available
	 * @throws BeansException in case of creation errors
	 * @since 5.0
	 * @see #getIfAvailable()
	 */
	/**
	 *返回对象的实例（可能是共享的或独立的）
	 *由这家工厂管理。
	 *@param defaultSupplier提供默认对象的回调
	 *如果工厂中没有
	 *@返回bean的实例或提供的默认对象
	 *如果没有这样的bean可用
	 *创建错误时@throws BeansException
	 *@自5.0
	 *@参见#getIfAvailable（）
	 */
	default T getIfAvailable(Supplier<T> defaultSupplier) throws BeansException {
		T dependency = getIfAvailable();
		return (dependency != null ? dependency : defaultSupplier.get());
	}

	/**
	 * Consume an instance (possibly shared or independent) of the object
	 * managed by this factory, if available.
	 * @param dependencyConsumer a callback for processing the target object
	 * if available (not called otherwise)
	 * @throws BeansException in case of creation errors
	 * @since 5.0
	 * @see #getIfAvailable()
	 */
	/**
	 *使用对象的实例（可能是共享的或独立的）
	 *如果可用，由该工厂管理。
	 *@param dependencyConsumer处理目标对象的回调
	 *如果可用（未另行调用）
	 *创建错误时@throws BeansException
	 *@自5.0
	 *@参见#getIfAvailable（）
	 */
	default void ifAvailable(Consumer<T> dependencyConsumer) throws BeansException {
		T dependency = getIfAvailable();
		if (dependency != null) {
			dependencyConsumer.accept(dependency);
		}
	}

	/**
	 * Return an instance (possibly shared or independent) of the object
	 * managed by this factory.
	 * @return an instance of the bean, or {@code null} if not available or
	 * not unique (i.e. multiple candidates found with none marked as primary)
	 * @throws BeansException in case of creation errors
	 * @see #getObject()
	 */
	/**
	 *返回对象的实例（可能是共享的或独立的）
	 *由这家工厂管理。
	 *@返回bean的实例，或者｛@code null｝（如果不可用），或者
	 *不唯一（即找到多个候选项，但没有标记为主要候选项）
	 *创建错误时@throws BeansException
	 *@参见#getObject（）
	 */
	@Nullable
	T getIfUnique() throws BeansException;

	/**
	 * Return an instance (possibly shared or independent) of the object
	 * managed by this factory.
	 * @param defaultSupplier a callback for supplying a default object
	 * if no unique candidate is present in the factory
	 * @return an instance of the bean, or the supplied default object
	 * if no such bean is available or if it is not unique in the factory
	 * (i.e. multiple candidates found with none marked as primary)
	 * @throws BeansException in case of creation errors
	 * @since 5.0
	 * @see #getIfUnique()
	 */
	/**
	 *返回对象的实例（可能是共享的或独立的）
	 *由这家工厂管理。
	 *@param defaultSupplier提供默认对象的回调
	 *如果工厂中没有唯一的候选人
	 *@返回bean的实例或提供的默认对象
	 *如果没有这样的bean可用，或者它在工厂中不是唯一的
	 *（即找到多个候选人，但没有一个被标记为主要候选人）
	 *创建错误时@throws BeansException
	 *@自5.0
	 *@参见#getIfUnique（）
	 */
	default T getIfUnique(Supplier<T> defaultSupplier) throws BeansException {
		T dependency = getIfUnique();
		return (dependency != null ? dependency : defaultSupplier.get());
	}

	/**
	 * Consume an instance (possibly shared or independent) of the object
	 * managed by this factory, if unique.
	 * @param dependencyConsumer a callback for processing the target object
	 * if unique (not called otherwise)
	 * @throws BeansException in case of creation errors
	 * @since 5.0
	 * @see #getIfAvailable()
	 */
	/**
	 *使用对象的实例（可能是共享的或独立的）
	 *由该工厂管理（如果唯一）。
	 *@param dependencyConsumer处理目标对象的回调
	 *如果唯一（否则不调用）
	 *创建错误时@throws BeansException
	 *@自5.0
	 *@参见#getIfAvailable（）
	 */
	default void ifUnique(Consumer<T> dependencyConsumer) throws BeansException {
		T dependency = getIfUnique();
		if (dependency != null) {
			dependencyConsumer.accept(dependency);
		}
	}

	/**
	 * Return an {@link Iterator} over all matching object instances,
	 * without specific ordering guarantees (but typically in registration order).
	 * @since 5.1
	 * @see #stream()
	 */
	/**
	 *返回所有匹配对象实例的｛@link Iterator｝，
	 *没有特定的订购保证（但通常按注册顺序）。
	 *@自5.1
	 *@参见#stream（）
	 */
	@Override
	default Iterator<T> iterator() {
		return stream().iterator();
	}

	/**
	 * Return a sequential {@link Stream} over all matching object instances,
	 * without specific ordering guarantees (but typically in registration order).
	 * @since 5.1
	 * @see #iterator()
	 * @see #orderedStream()
	 */
	/**
	 *返回所有匹配对象实例的顺序｛@link Stream｝，
	 *没有特定的订购保证（但通常按注册顺序）。
	 *@自5.1
	 *@参见#迭代器（）
	 *@参见#orderedStream（）
	 */
	default Stream<T> stream() {
		throw new UnsupportedOperationException("Multi element access not supported");
	}

	/**
	 * Return a sequential {@link Stream} over all matching object instances,
	 * pre-ordered according to the factory's common order comparator.
	 * <p>In a standard Spring application context, this will be ordered
	 * according to {@link org.springframework.core.Ordered} conventions,
	 * and in case of annotation-based configuration also considering the
	 * {@link org.springframework.core.annotation.Order} annotation,
	 * analogous to multi-element injection points of list/array type.
	 * @since 5.1
	 * @see #stream()
	 * @see org.springframework.core.OrderComparator
	 */
	/**
	 *返回所有匹配对象实例的顺序｛@link Stream｝，
	 *根据工厂的普通订单比较器预先订购。
	 *＜p＞在标准的Spring应用程序上下文中
	 *根据{@link.org.springframework.core.Ordered}约定，
	 *并且在基于注释的配置的情况下，还考虑
	 *｛@link org.springframework.core.annotation.Order｝annotation，
	 *类似于列表/数组类型的多元素注入点。
	 *@自5.1
	 *@参见#stream（）
	 *@参见org.springframework.core.OrderComparator
	 */
	default Stream<T> orderedStream() {
		throw new UnsupportedOperationException("Ordered element access not supported");
	}

}
